<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>1v1 视频语音通话</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.1.3/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .hero-section {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
        }
        .room-card {
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        .call-option {
            border: 2px solid #e9ecef;
            border-radius: 10px;
            padding: 20px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        .call-option:hover {
            border-color: #007bff;
            transform: translateY(-2px);
        }
        .call-option.selected {
            border-color: #007bff;
            background: #f8f9ff;
        }
        .call-icon {
            font-size: 48px;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <div class="hero-section">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-md-8">
                    <div class="room-card p-5">
                        <h2 class="text-center mb-4">🎥 1v1 视频语音通话</h2>

                        <!-- 创建房间表单 -->
                        <div class="mb-4">
                            <form id="createRoomForm">
                                <div class="mb-3">
                                    <label class="form-label">房间名称</label>
                                    <input type="text" class="form-control" id="roomName" value="1v1课堂" required>
                                </div>
                                <button type="submit" class="btn btn-primary w-100" id="createButton">
                                    📹 创建房间
                                </button>
                            </form>
                        </div>

                        <hr>

                        <div>
                            <form id="joinRoomForm">
                                <div class="mb-3">
                                    <label class="form-label">房间ID</label>
                                    <input type="text" class="form-control" id="roomId" placeholder="输入房间ID" required>
                                </div>
                                <button type="submit" class="btn btn-success w-100">加入房间</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>

        // 创建房间
        document.getElementById('createRoomForm').addEventListener('submit', async (e) => {
            e.preventDefault();

            const roomName = document.getElementById('roomName').value;

            try {
                const response = await fetch('http://localhost:8000/api/v1/rooms', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        room_name: roomName
                    })
                });

                const data = await response.json();
                const room = data.data

                const selectedData = {
                    code: room.code,
                    name: room.name,
                    user_id: room.user_id,
                    user_a: room.user_a
                };
                const params = new URLSearchParams(selectedData).toString();

                if (data.code==0) {
                    window.location.href = `http://localhost:8000/room?${params}`;
                } else {
                    alert('创建房间失败');
                }
            } catch (error) {
                alert('网络错误: ' + error.message);
            }
        });

        // 加入房间
        document.getElementById('joinRoomForm').addEventListener('submit', async (e) => {
            e.preventDefault();

            const roomId = document.getElementById('roomId').value;

            try {
                const response = await fetch(`http://localhost:8000/api/v1/rooms/${roomId}/join`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    }
                });

                const data = await response.json();
                const room = data.data

                const selectedData = {
                    code: room.code,
                    name: room.name,
                    user_id: room.user_id
                };
                const params = new URLSearchParams(selectedData).toString();

                if (data.code==0) {
                    window.location.href = `http://localhost:8000/room?${params}`;
                } else {
                    alert('加入房间失败');
                }
            } catch (error) {
                alert('房间不存在或网络错误');
            }
        });
    </script>
</body>
</html>